package ch.lue.tvsedit.swing;

import javax.swing.AbstractListModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JList;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import ch.lue.tvsedit.model.ChannelGroup;

public class ListListModel extends AbstractListModel implements ListSelectionListener
{
	private ListModel model;
	private ChannelGroup actualGroup;
	private JList list;

	public ListListModel(ListSelectionModel selectionModel, JList jList) 
	{
		selectionModel.addListSelectionListener(this);
		this.model = jList.getModel();
		this.list = jList;
		
	}

	@Override
	public Object getElementAt(int index) 
	{
		if(actualGroup == null)
			return null;
		return actualGroup.getElementAt(index);
	}

	@Override
	public int getSize() 
	{
		if(actualGroup == null)
			return 0;
		return actualGroup.getSize();
	}

	@Override
	public void valueChanged(ListSelectionEvent e) 
	{
		int oldSize = 0;
		if(actualGroup != null)
			oldSize = actualGroup.getSize();

		actualGroup = (ChannelGroup)model.getElementAt(list.getSelectedIndex());
		if(oldSize <= actualGroup.getSize())
		{
			fireContentsChanged(this, 0,actualGroup.getSize());
			System.out.println(0+" : "+actualGroup.getSize());
		}
		else
		{
			fireContentsChanged(this, 0,oldSize);
			System.out.println(0+" : "+oldSize);
		}
	}
	
	
}
